package com.springboot.rigger.modules.controller;

import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import java.io.FileInputStream;
import java.io.IOException;

public class IppPrint {
    public void PrintImage(String filename) {

        try {

            //获得打印属性
            PrintRequestAttributeSet prass = new HashPrintRequestAttributeSet();
            prass.add(new Copies(1));
            //获得打印设备
            PrintService[] pss = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, prass);
            if (pss.length == 0)
                throw new RuntimeException("No printer services available.");
            PrintService ps = pss[0];
            System.out.println("Printing to" + ps);
            //获得打印工作
            DocPrintJob job = ps.createPrintJob();

            FileInputStream fin = new FileInputStream(filename);
            Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
            //开始打印
            job.print(doc, prass);
            fin.close();

        } catch (IOException ie) {
            ie.printStackTrace();
        } catch (PrintException pe) {
            pe.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
//        if (args.length < 1) {
//            System.err.println("Usage:java PrintImage<image name>");
//            System.exit(1);
//
//        }
        IppPrint ippPrint = new IppPrint();
        ippPrint.PrintImage("C:\\Users\\Administrator\\Downloads\\48a6b231d7aecce3a556812e1616f4a0.jpeg");

    }
}